package com.claimSettlement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.claimSettlement.mapper.SurveyLossMapper;
import com.claimSettlement.pojo.SurveyLoss;
import com.claimSettlement.service.SurveyLossService;
import com.claimSettlement.utils.JsonUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查勘与定损业务层
 * 曹文韬 -- 2021-05-20
 */
@Service
public class SurveyLossServiceImpl implements SurveyLossService {
    @Resource
    SurveyLossMapper surveyLossMapper;

    @Resource
    RedisTemplate<String,Object> redisTemplate;

    public List<SurveyLoss> getSurveyLossAll() {
        redisTemplate.delete("surveList");
        List<SurveyLoss> list = null;
        if(redisTemplate.hasKey("surveList")){
            list = JsonUtils.jsonToList(redisTemplate.opsForHash().values("surveList").toString(),SurveyLoss.class);
        }else {
            list = surveyLossMapper.getSurveyLossAll();
            Map<String,String> map = new HashMap();
            for (SurveyLoss surveyLoss: list) {
                map.put(String.valueOf(surveyLoss.getSurveyLossId()),JsonUtils.objectToJson(surveyLoss));
            }
            redisTemplate.opsForHash().putAll("surveList",map);
        }
        return list;
    }

    public List<SurveyLoss> getSurveyLossIdOrCarframeId(int id, String carframeId) {
        if(id == 0 && (carframeId.length() == 0 || carframeId == "")){
            return this.getSurveyLossAll();
        }
        return surveyLossMapper.getSurveyLossIdOrCarframeId(id, carframeId);
    }

    public int insSurveyLoss(SurveyLoss surveyLoss) {
        int count = surveyLossMapper.insert(surveyLoss);
        if(count > 0){
            redisTemplate.opsForHash().put("surveList",surveyLoss.getSurveyLossId(),JsonUtils.objectToJson(surveyLoss));
        }
        return count;
    }

    public int delSurveyLoss(String caseId) {
        QueryWrapper<SurveyLoss> queryWrapper = new QueryWrapper();
        queryWrapper.eq("caseId",caseId);
        int count = surveyLossMapper.delete(queryWrapper);
        if(count > 0){
            redisTemplate.delete("surveList");
        }
        return count;
    }

    public int updSurveyLossProspectState(SurveyLoss surveyLoss) {
        int count = surveyLossMapper.updSurveyLossProspectState(surveyLoss);
        if(count > 0){
            SurveyLoss surveyLoss1 = JsonUtils.jsonToPojo(redisTemplate.opsForHash().get("surveList",String.valueOf(surveyLoss.getSurveyLossId())).toString(),SurveyLoss.class);
            surveyLoss1.setProspectState(surveyLoss.getProspectState());
            redisTemplate.opsForHash().put("surveList",surveyLoss.getSurveyLossId(),JsonUtils.objectToJson(surveyLoss1));
        }
        return count;
    }

    public int updSurveyLossSettUserId(SurveyLoss surveyLoss) {
        return surveyLossMapper.updSurveyLossSettUserId(surveyLoss);
    }
}
